﻿using Super.Hmi.Global;
using System.Windows;
using System.Windows.Media;

namespace Super.Hmi.Utils.ThemeManager
{
    /// <summary>
    /// 外观设置
    /// </summary>
    public class AppearanceSettings : ThemeViewModelBase
    {
        private Theme _theme = new Theme();
        /// <summary>
        /// 主题
        /// </summary>
        public Theme Theme
        {
            get => _theme;
            set => SetProperty(ref _theme, value);
        }
        private BuiltInThemes _builtInThemes = new BuiltInThemes();
        /// <summary>
        /// 内置颜色
        /// </summary>
        public BuiltInThemes BuiltInThemes
        {
            get => _builtInThemes;
            set => SetProperty(ref _builtInThemes, value);
        }
        private CornerRadiusType _cornerRadiusType = new CornerRadiusType();
        /// <summary>
        /// 圆角
        /// </summary>
        public CornerRadiusType CornerRadiusType
        {
            get => _cornerRadiusType;
            set => SetProperty(ref _cornerRadiusType, value);
        }
        private Other _other = new Other();
        /// <summary>
        /// 其他
        /// </summary>
        public Other Other
        {
            get => _other;
            set => SetProperty(ref _other, value);
        }
    }
    /// <summary>
    /// 主题
    /// </summary>
    public class Theme : ThemeViewModelBase
    {
        private ThemeType _themeType = ThemeType.Light;
        /// <summary>
        /// 主题
        /// </summary>
        public ThemeType ThemeType
        {
            get => _themeType;
            set
            {
                SetProperty(ref _themeType, value);
                SetThemeType(value);
            }
        }
        private bool _darkSidebar = false;
        /// <summary>
        /// 深色侧边栏
        /// </summary>
        public bool DarkSidebar
        {
            get => _darkSidebar;
            set
            {
                SetProperty(ref _darkSidebar, value);
            }
        }
        private bool _darkTopBar = false;
        /// <summary>
        /// 深色顶栏
        /// </summary>
        public bool DarkTopBar
        {
            get => _darkTopBar;
            set => SetProperty(ref _darkTopBar, value);
        }
        /// <summary>
        /// 设置主题
        /// </summary>
        /// <param name="type"></param>
        private void SetThemeType(ThemeType type)
        {
            var dict = Application.Current.Resources.MergedDictionaries;
            ResourceDictionary darkThemeRes = new ResourceDictionary { Source = new Uri(FilePath.DarkThemeUri, UriKind.Absolute) };
            ResourceDictionary defualtThemeRes = new ResourceDictionary { Source = new Uri(FilePath.DefualtThemeUri, UriKind.Absolute) };
            ResourceDictionary themeRes = new ResourceDictionary { Source = new Uri(FilePath.ThemeUri, UriKind.Absolute) };
            if (type == ThemeType.Dark)
            {
                // 查找并移除默认主题（如果存在）
                var defaultThemeToRemove = dict?.FirstOrDefault(d => d.Source != null && d.Source.OriginalString.Contains(FilePath.DefualtThemeUri));
                dict?.Remove(defaultThemeToRemove);
                dict?.Add(darkThemeRes);
            }
            else
            {
                var darkThemeToRemove = dict?.FirstOrDefault(d => d.Source != null && d.Source.OriginalString.Contains(FilePath.DarkThemeUri));
                dict?.Remove(darkThemeToRemove);
                dict?.Add(defualtThemeRes);
            }
            var themeToRemove = dict?.FirstOrDefault(d => d.Source != null && d.Source.OriginalString.Contains(FilePath.ThemeUri));
            dict?.Remove(themeToRemove);
            dict?.Add(themeRes);
        }
    }
    /// <summary>
    /// 内置颜色
    /// </summary>
    public class BuiltInThemes : ThemeViewModelBase
    {
        private string _primaryColor = "#006BE6";
        /// <summary>
        /// 主色调
        /// </summary>
        public string PrimaryColor
        {
            get { return _primaryColor; }
            set
            {
                SetProperty<string>(ref _primaryColor, value);
                Application.Current.Resources["PrimaryBrush"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString(value));
            }
        }
    }
    /// <summary>
    /// 圆角
    /// </summary>
    public class CornerRadiusType : ThemeViewModelBase
    {
        private double _cornerRadius = 5;
        /// <summary>
        /// 圆角
        /// </summary>
        public double CornerRadius
        {
            get => _cornerRadius;
            set
            {
                SetProperty(ref _cornerRadius, value);
                SetCornerRadius(value);
            }
        }
        // 设置默认圆角 必须覆盖 HandyControl 有BUG 改不了
        private void SetCornerRadius(double radius)
        {
            var dict = Application.Current.Resources.MergedDictionaries;
            ResourceDictionary themeRes = new ResourceDictionary { Source = new Uri(FilePath.ThemeUri, UriKind.Absolute) };
            var themeToRemove = dict?.FirstOrDefault(d => d.Source != null && d.Source.OriginalString.Contains(FilePath.ThemeUri));
            dict?.Remove(themeToRemove);
            dict?.Add(themeRes);
            var radiusToRemove = dict?.FirstOrDefault(d => d.Contains("DefaultCornerRadius"));
            dict?.Remove(radiusToRemove);
            ResourceDictionary cornerRadiustRes = new ResourceDictionary();
            cornerRadiustRes["DefaultCornerRadius"] = new CornerRadius(ThemeSettingHelper.Instance.ThemeConfig.AppearanceSettings.CornerRadiusType.CornerRadius);
            dict?.Add(cornerRadiustRes);
        }
    }
    /// <summary>
    /// 其他
    /// </summary>
    public class Other : ThemeViewModelBase
    {
        private bool _colorWeaknessMode = false;
        /// <summary>
        /// 色弱模式
        /// </summary>
        public bool ColorWeaknessMode
        {
            get => _colorWeaknessMode;
            set => SetProperty(ref _colorWeaknessMode, value);
        }
        private bool _greyMode = false;
        /// <summary>
        /// 灰色模式
        /// </summary>
        public bool GreyMode
        {
            get => _greyMode;
            set => SetProperty(ref _greyMode, value);
        }
    }
    /// <summary>
    /// 主题类型
    /// </summary>
    public enum ThemeType
    {
        Light = 0,
        Dark = 1,
    }
}
